package br.net.woodstock.tickettoride.struts2.web.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import br.net.woodstock.rockframework.web.struts2.Struts2Constants;
import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.spring.web.AbstractApplicationAction;

@Controller
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class LoginAction extends AbstractApplicationAction {

	private static final long	serialVersionUID	= -6100113189336267042L;

	private String				login;

	private String				password;

	@Autowired(required = true)
	private UserService			userService;

	public String logon() throws Exception {
		User user = this.userService.getByLoginPassword(this.login, this.password);

		if (user == null) {
			return Struts2Constants.ERROR;
		}

		SecurityUtils.setSessionUser(user);

		return Struts2Constants.INDEX;
	}

	public String getLogin() {
		return this.login;
	}

	public void setLogin(final String login) {
		this.login = login;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

}
